/*
 * create by Littleken at 2019/3/18 14:05
 */
package com.littleken.sellproject.utils.converter;

import com.fasterxml.jackson.databind.JsonSerializer;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 用于时间的格式转换，用在OrderDTO的注释上
 */
public class Date2LongSerializer extends JsonSerializer<Date> {

    //从数据库读出来是 2019-03-15T02:49:21.000+0000 这样的
/*    public void parseDatetest(String dateStr) throws ParseException {
        //String dateStr = "2019-03-15T02:49:21.000+0000";
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSZ");
        Date result;
        result = df.parse(dateStr);
        System.out.println(result.getTime());
    }*/

    @Override
    public void serialize(Date date, com.fasterxml.jackson.core.JsonGenerator jsonGenerator, com.fasterxml.jackson.databind.SerializerProvider serializerProvider) throws IOException {
        //时间戳转换为以秒为单位
        jsonGenerator.writeNumber(date.getTime() / 1000);
    }
}
